﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CSequence;

namespace Sequence
{
    public partial class usStep : UserControl
    {
        public usStep()
        {
            InitializeComponent();
        }

        CStep m_Step;
        private eShowType m_iShowType;
        public bool bHasFocus;

        public enum eShowType
        {
            Names = 0,
            Values = 1,
        }

        public eShowType ShowType
        {
            get
            {
                return (eShowType)m_iShowType;
            }
            set
            {
                m_iShowType = value;
                foreach (CInput oIn in m_Step.lInputs)
                {
                    if (m_iShowType == eShowType.Names)
                    {
                        panel1.Controls[oIn.iControlIndex].Text = oIn.sName;                       
                    }
                    else
                    {
                        panel1.Controls[oIn.iControlIndex].Text = oIn.sCalValue;
                    }

                }
                foreach (COutput oOut in m_Step.lOutputs)
                {
                    if (m_iShowType == eShowType.Names)
                        panel1.Controls[oOut.iControlIndex].Text = oOut.sName;
                    else
                        panel1.Controls[oOut.iControlIndex].Text = oOut.sCalValue;

                }          
            }
        }

        public void SetLocation()
        {
            this.Width = (int)(Convert.ToDouble(m_Step.GetDisplayValue("Width","100")));
            this.Height = (int)(Convert.ToDouble(m_Step.GetDisplayValue("Height","100")));
            panel1.Height = this.Height - 10;
            this.Top = (int)(Convert.ToDouble(m_Step.GetDisplayValue("Y","15")));
            this.Left = (int)(Convert.ToDouble(m_Step.GetDisplayValue("X","15")));
        }

        public int GetInputOffset(string sName)
        {
            int iRet = 0;
            foreach (CInput oIn in m_Step.lInputs)
            {
                if (oIn.sName == sName)
                {
                    iRet = panel1.Controls[oIn.iControlIndex].Top + 45;
                    break;
                }
            }
            return iRet;
        }

        public int GetOutputOffset(string sName)
        {
            int iRet = 0;
            foreach (COutput oOut in m_Step.lOutputs)
            {
                if (oOut.sName == sName)
                {
                    iRet = panel1.Controls[oOut.iControlIndex].Top + 45;
                    break;
                }
            }
            return iRet;
        }

        public usStep(CStep oStep, ContextMenuStrip oContextMenuStrip)
        { 
            InitializeComponent();
            Display(oStep, oContextMenuStrip);            
        }

        public void Display(CStep oStep, ContextMenuStrip oContextMenuStrip)
        {
            m_Step = oStep;
            SetLocation();
            tbName.Text = oStep.sName;
            tbID.Text = oStep.sId;
            this.ContextMenuStrip = oContextMenuStrip;
            tbName.ContextMenuStrip = oContextMenuStrip;
            base.Text = oStep.sName;

            if (oStep.sType == "Constants")
                pictureBox1.Image = Properties.Resources.Constant;
            else if (oStep.sType == "QueryDef")
                pictureBox1.Image = Properties.Resources.QueryDef;
            else if (oStep.sType == "AlisWebService")
                pictureBox1.Image = Properties.Resources.AlisWebService;
            else if (oStep.sType == "IF")
            {
                pictureBox1.Image = Properties.Resources.IF;
                panel1.BackColor = Color.LightGreen;
            }
            else if (oStep.sType == "ELSE")
            {
                pictureBox1.Image = Properties.Resources.IF;
                panel1.BackColor = Color.LightGreen;
            }
            else if (oStep.sType == "END IF")
            {
                pictureBox1.Image = Properties.Resources.IF;
                panel1.BackColor = Color.LightGreen;
            }
            else if (oStep.sType == "QueryDefProcess")
            {
                pictureBox1.Image = Properties.Resources.Sequence16;
                panel1.BackColor = Color.LightSteelBlue;
            }

            else if (oStep.sType == "FOR")
            {
                pictureBox1.Image = Properties.Resources.IF;
                panel1.BackColor = Color.LimeGreen;
            }
            else if (oStep.sType == "END FOR")
            {
                pictureBox1.Image = Properties.Resources.IF;
                panel1.BackColor = Color.LimeGreen;
            }

            panel2.BackColor = panel1.BackColor;
            int i = 0;

            panel1.Controls.Clear();
            foreach (CInput oIn in oStep.lInputs)
            {
                TextBox dynamicTB = new TextBox();
                dynamicTB.ContextMenuStrip = this.ContextMenuStrip;
                dynamicTB.Top =  20 * i; dynamicTB.Left = 5; dynamicTB.Width = (panel1.Width - 10) / 2;
                dynamicTB.Text = oIn.sName;
                dynamicTB.ReadOnly = true;
                panel1.Controls.Add(dynamicTB);
                oIn.iControlIndex = panel1.Controls.Count - 1;
                i++;
            }

            i = 0;

            foreach (COutput oOut in oStep.lOutputs)
            {
                TextBox dynamicTB = new TextBox();
                dynamicTB.ContextMenuStrip = this.ContextMenuStrip;
                dynamicTB.Top = 20 * i; dynamicTB.Left = 100; dynamicTB.Width = (panel1.Width - 10) / 2;
                dynamicTB.Text = oOut.sName;
                dynamicTB.ReadOnly = true;
                panel1.Controls.Add(dynamicTB);
                oOut.iControlIndex = panel1.Controls.Count - 1;
                i++;
            }
        }

        private void usStep_Leave(object sender, EventArgs e)
        {
            tbName.Font = new Font(tbName.Font, FontStyle.Regular);
            bHasFocus = false;
        }

        private void usStep_Enter(object sender, EventArgs e)
        {
            tbName.Font = new Font(tbName.Font, FontStyle.Bold);
            bHasFocus = true; 
        }

        private void tbName_TextChanged(object sender, EventArgs e)
        {

        }

        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            tbName.Focus();
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            tbName.Focus();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void panel2_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
